package com.dropTables.TakingNote;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.Button;
import android.widget.EditText;

import java.util.regex.Pattern;

public class RegisterActivity extends Activity {
	
	private Button confirm_reg_button;
	private Button cancel_reg_button;
	private EditText username_enter;
	private EditText email_enter;
	private EditText email_confirm;
	private EditText password_enter;
	private EditText password_confirm;
	
	private int usernameMinLen = 6;
	private int passwordMinLen = 6;
	
	
	static final int mismEmail = 0;
	static final int invalEmail = 1;
	static final int mismPass = 2;
	static final int unLen = 3;
	static final int passLen = 4;
	static final int unTaken = 5;
	static final int emailInval = 6;
	
	static final String username_error = "duplicate username";
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		
		confirm_reg_button = (Button) findViewById(R.id.register_conf_button);			//Match buttons to their id's
		cancel_reg_button = (Button) findViewById(R.id.register_cancel_button);
		
		username_enter = (EditText) findViewById(R.id.register_username);
		
		email_enter = (EditText) findViewById(R.id.register_email);
		email_confirm = (EditText) findViewById(R.id.register_email_conf);
		
		password_enter = (EditText) findViewById(R.id.register_password);
		password_confirm = (EditText) findViewById(R.id.register_password_conf);
		
		confirm_reg_button.setOnClickListener(new View.OnClickListener() {				//When the user tries to register call checkInfo
			@Override
			public void onClick(View v) {
				checkInfo();
			}
		});
		
		cancel_reg_button.setOnClickListener(new View.OnClickListener() {				//When user presses cancel, cancel
			@Override
			public void onClick(View v) {
				returnCancel();
			}
		});
	}
	
	private void returnCancel(){				//Cancel
		setResult(RESULT_CANCELED);
		finish();
	}
	
	private void returnRegistered(){
		setResult(RESULT_OK);
		finish();
	}
	
	private void checkInfo(){
		String un, email, emailConf, pass, passConf;
		un = username_enter.getText().toString();			//Get info from EditTexts 
		email = email_enter.getText().toString();
		emailConf = email_confirm.getText().toString();
		pass = password_enter.getText().toString();
		passConf = password_confirm.getText().toString();
		
		if(un.length() < usernameMinLen){							//Check minimum length for username
			Toast.makeText(getApplicationContext(), "Error: Username must be at least " + usernameMinLen + " characters.", 1).show();
			username_enter.setText("");
			passClear();
		}
		else if(!email.equals(emailConf)){							//Check email matches confirmation
			Toast.makeText(getApplicationContext(), "Error: Email does not match confirmation.", 1).show();
			emClear();
			passClear();
		}
		else if(!Pattern.matches("(\\w+\\.)*\\w+@(\\w+\\.)*[a-z]{2,4}$", email)){				//Check email is ok
			Toast.makeText(getApplicationContext(), "Error: Invalid email.", 1).show();
			emClear();
			passClear();
		}
		else if(pass.length() < passwordMinLen){												//Check password is min length
			Toast.makeText(getApplicationContext(), "Error: Password must be at least " + passwordMinLen + " characters.", 1).show();
			passClear();
		}
		else if(!pass.equals(passConf)){														//Check password matches confirmation
			Toast.makeText(getApplicationContext(), "Error: Password does not match confirmation.", 1).show();
			passClear();
		}
		else if(server_register(un,email,pass)){ 				//Try registering
				returnRegistered();
		}
	}
	

	private void emClear(){
		email_confirm.setText("");
	}
	private void passClear(){
		password_enter.setText("");
		password_confirm.setText("");
	}
	
	
	
	//A function that sends username, email, and password of a possible account to the server to be checked.
	private Boolean server_register(String un, String email, String pass){
		String response = "";
		try{
			response = HttpComm.executeHttpregister(un, pass, email);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		if(response.equals(username_error)){					
			Toast.makeText(this, "Username already exists.", 1).show();
			return false;
		}
	return true;
		
	}

}
